package com.cll.prototype.user.service.impl;

import com.cll.prototype.api.user.entity.User;
import com.cll.prototype.api.user.mapper.UserMapper;
import com.cll.prototype.api.user.service.ILoginTokenService;
import com.cll.prototype.api.user.service.IUserService;
import com.cll.prototype.common.dictionary.AjaxResultEnum;
import com.cll.prototype.common.dto.AjaxResult;
import com.cll.prototype.common.util.EncryptUtils;
import com.cll.prototype.common.util.ValidateUtils;
import com.cll.prototype.user.controller.service.captcha.AuthCaptchaControllerFeignClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 描述信息:
 *
 * @author CLL
 * @version 1.0
 * @date 2020/11/8 15:13
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ILoginTokenService loginTokenService;

    @Qualifier("authCaptchaControllerFeignClient")
    @Autowired
    private AuthCaptchaControllerFeignClient authCaptchaControllerFeignClient;

    @Override
    public AjaxResult add(String email, String password, String confirmPassword, String captcha) {
        if (StringUtils.isAnyBlank(email, password, confirmPassword)) {
            return AjaxResult.error(41, "数据不合法");
        }
        if (!ValidateUtils.checkEmailAddress(email)) {
            return AjaxResult.error(43, "邮箱地址不合法");
        }
        if (!password.equals(confirmPassword)) {
            return AjaxResult.error(42, "两次密码不一致");
        }
        // 检测邮箱是否已经注册
        AjaxResult registryResult = isRegistry(email);
        if (1 == (Integer) registryResult.getData()) {
            return AjaxResult.error(43, "当前邮箱已经注册");
        }
        // 检测用户输入的邮箱验证码是否正确 TODO 未实现远程调用验证码微服务
        AjaxResult ajaxResult = authCaptchaControllerFeignClient.checkCaptcha(email, captcha);
        boolean captchaFlag = null != ajaxResult && AjaxResultEnum.SUCCESS.getCode() == ajaxResult.getCode() && null != ajaxResult.getData() && (1 == (Integer) ajaxResult.getData());
        if (captchaFlag) {
            String encodePassword = EncryptUtils.encryptUseMD5(password);
            if (StringUtils.isBlank(encodePassword)) {
                return AjaxResult.error("加密密码失败");
            }
            User user = new User();
            user.setEmail(email);
            user.setGmtCreate(LocalDateTime.now());
            user.setPassword(encodePassword);
            AjaxResult addResult = add(user);
            if (AjaxResultEnum.SUCCESS.getCode() == addResult.getCode()) {
                return loginTokenService.generateLoginToken(email);
            }
            return addResult;
        } else {
            return ajaxResult;
        }
    }

    @Override
    public AjaxResult add(User user) {
        int insertRecord = userMapper.add(user);
        if (1 == insertRecord) {
            return AjaxResult.success(20, "新增用户成功");
        }
        return AjaxResult.error(50, "新增用户失败");
    }

    @Override
    public AjaxResult isRegistry(String email) {
        if (!ValidateUtils.checkEmailAddress(email)) {
            return AjaxResult.error(43, "邮箱地址不合法");
        }
        User user = userMapper.getByEmail(email);
        return null == user ? AjaxResult.success(0) : AjaxResult.success(1);
    }

    @Override
    public AjaxResult login(String email, String password) {
        if (StringUtils.isAnyBlank(email, password)) {
            return AjaxResult.error(41, "数据不合法");
        }
        if (!ValidateUtils.checkEmailAddress(email)) {
            return AjaxResult.error(43, "邮箱地址不合法");
        }
        User user = userMapper.getByEmail(email);
        if (null == user) {
            return AjaxResult.error(42, "用户不存在");
        }
        String encodePassword = EncryptUtils.encryptUseMD5(password);
        if (StringUtils.isBlank(encodePassword)) {
            return AjaxResult.error(43, "加密密码失败");
        }

        String sourcePassword = user.getPassword();
        if (!encodePassword.equals(sourcePassword)) {
            return AjaxResult.error(44, "密码错误");
        }
        return loginTokenService.generateLoginToken(email);
    }
}
